
// 实现一个进度条

#include"processBar.h"

char bar[102] = {0};
const char *lable = "|/-\\";

//void Processbar()
void Processbar(int speed)
{
    int cnt = 0;
    int len = strlen(lable);

    while( cnt <= 100)
    {
        printf("[%-100s][%d%%][%c]\r",bar,cnt,lable[cnt%len]);
        
        fflush(stdout);
        bar[cnt++] = STYLE;
        if(cnt < 100)     bar[cnt] = RIGHT;
        //usleep(70000);
        usleep(speed);
    }
    printf("\n");
}


//方法二
//单次进度调用

//void Processbar_sigle(int rate)
//{
//    if(rate < 0 || rate > 100)
//        return;
//    int len = strlen(lable);
//    printf("[%-100s][%d%%][%c]\r",bar,rate,lable[rate%len]);
//    fflush(stdout);
//    bar[rate++] = STYLE;
//    if(rate < 100)  bar[rate] = RIGHT;
//}

void Processbar_sigle(int rate, int color)
{
    if(rate < 0 || rate > 100)
        return;
    int len = strlen(lable);
    switch(color)
    {
        case 31:
            printf(COLOR1"[%-100s]"NONE"[%d%%][%c]\r",bar,rate,lable[rate%len]);
            break;
        case 32:
            printf(COLOR2"[%-100s]"NONE"[%d%%][%c]\r",bar,rate,lable[rate%len]);
            break;
        case 33:
            printf(COLOR3"[%-100s]"NONE"[%d%%][%c]\r",bar,rate,lable[rate%len]);
            break;
        case 34:
            printf(COLOR4"[%-100s]"NONE"[%d%%][%c]\r",bar,rate,lable[rate%len]);
            break;
    }
    fflush(stdout);
    bar[rate++] = STYLE;
    if(rate < 100)  bar[rate] = RIGHT;
}


void InitBar()
{
    memset(bar, '\0', sizeof(bar));
}





